﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using BUS;
using DTO;

namespace GUI
{
    public partial class frmTraCuuKhachHang : Form
    {
        public frmTraCuuKhachHang()
        {
            InitializeComponent();
        }

        private void radtatca_CheckedChanged(object sender, EventArgs e)
        {
            this.txttimkiem.Text = "";
            this.txttimkiem.Focus();
            hienthidanhsach();
        }

        private void radtenkh_CheckedChanged(object sender, EventArgs e)
        {
            this.txttimkiem.Text = "";
            this.txttimkiem.Focus();
            timkiemtenkh();
        }

        private void raddiachi_CheckedChanged(object sender, EventArgs e)
        {
            this.txttimkiem.Text = "";
            this.txttimkiem.Focus();
            timkiemdiachi();
        }

        private void raddienthoai_CheckedChanged(object sender, EventArgs e)
        {
            txttimkiem.Text = "0";
            this.txttimkiem.Focus();
            timkiemdienthoai();
        }

        private void rademail_CheckedChanged(object sender, EventArgs e)
        {
            this.txttimkiem.Text = "";
            this.txttimkiem.Focus();
            timkiememail();
        }

        private void txttimkiem_TextChanged(object sender, EventArgs e)
        {
            if (radtenkh.Checked == true)
            {
                timkiemtenkh();
            }
            if (raddiachi.Checked == true)
            {
                timkiemdiachi();
            }
            if (raddienthoai.Checked == true)
            {
                timkiemdienthoai();
            }
            if (rademail.Checked == true)
            {
                timkiememail();
            }
        }

        private void frmTraCuuKhachHang_Load(object sender, EventArgs e)
        {
            hienthidanhsach();
            this.radtatca.Checked = true;
        }

        public void hienthidanhsach()
        {
            List<KhachHangDTO> ds;
            KhachHangBUS bus = new KhachHangBUS();
            ds = bus.GetKhachHangAll();
            this.dskhachhang.Rows.Clear();
            for (int i = 0; i <= ds.Count - 1; i++)
            {
                this.dskhachhang.Rows.Add(ds[i].MaKhachHang, ds[i].TenKhachHang, ds[i].SoDienThoai, ds[i].Email, ds[i].DiaChi);
            }
        }

        private void btnlammoi_Click(object sender, EventArgs e)
        {
            this.txttimkiem.Text = "";
            this.raddiachi.Checked = false;
            this.raddienthoai.Checked = false;
            this.rademail.Checked = false;
            this.radtenkh.Checked = false;
            this.radtatca.Checked = true;
        }

        private void btnexit_Click(object sender, EventArgs e)
        {
            //DialogResult dlr = MessageBox.Show("Bạn muốn thoát khỏi chương trình?", "Warnning", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            //if (dlr == DialogResult.Yes)
            //    this.Close();
            Close();
        }
        public void timkiemtenkh()
        {
            List<KhachHangDTO> ds;
            KhachHangDTO kh = new KhachHangDTO();
            kh.TenKhachHang = this.txttimkiem.Text;
            KhachHangBUS bus = new KhachHangBUS();
            ds = bus.SelectKhachHangLikeTen(kh);
            this.dskhachhang.Rows.Clear();
            for (int i = 0; i <= ds.Count - 1; i++)
            {
                this.dskhachhang.Rows.Add(ds[i].MaKhachHang, ds[i].TenKhachHang, ds[i].SoDienThoai, ds[i].Email, ds[i].DiaChi);
            }
        }
        public void timkiemdiachi()
        {
            List<KhachHangDTO> ds;
            KhachHangDTO kh = new KhachHangDTO();
            kh.DiaChi = this.txttimkiem.Text;
            KhachHangBUS bus = new KhachHangBUS();
            ds = bus.SelectKhachHangLikeDiaChi(kh);
            this.dskhachhang.Rows.Clear();
            for (int i = 0; i <= ds.Count - 1; i++)
            {
                this.dskhachhang.Rows.Add(ds[i].MaKhachHang, ds[i].TenKhachHang, ds[i].SoDienThoai, ds[i].Email, ds[i].DiaChi);
            }
        }
        public void timkiemdienthoai()
        {
            List<KhachHangDTO> ds;
            KhachHangDTO kh = new KhachHangDTO();
            kh.SoDienThoai = this.txttimkiem.Text;
            KhachHangBUS bus = new KhachHangBUS();
            ds = bus.SelectKhachHangLikeDienThoai(kh);
            this.dskhachhang.Rows.Clear();
            for (int i = 0; i <= ds.Count - 1; i++)
            {
                this.dskhachhang.Rows.Add(ds[i].MaKhachHang, ds[i].TenKhachHang, ds[i].SoDienThoai, ds[i].Email, ds[i].DiaChi);
            }
        }
        public void timkiememail()
        {
            List<KhachHangDTO> ds;
            KhachHangDTO kh = new KhachHangDTO();
            kh.Email = this.txttimkiem.Text;
            KhachHangBUS bus = new KhachHangBUS();
            ds = bus.SelectKhachHangLikeEmail(kh);
            this.dskhachhang.Rows.Clear();
            for (int i = 0; i <= ds.Count - 1; i++)
            {
                this.dskhachhang.Rows.Add(ds[i].MaKhachHang, ds[i].TenKhachHang, ds[i].SoDienThoai, ds[i].Email, ds[i].DiaChi);
            }
        }
    }
}
